﻿using Microsoft.AspNetCore.Mvc;
using StarsAdmin.Application.Auth.Dtos;
using StarsAdmin.Application.Auth.Services;
using StarsAdmin.Application.Menu.Dtos;
using StarsAdmin.Core.Attributes;
using StarsAdmin.Core.Models;
using StarsAdmin.Web.Core.Conttrollers;
using StarsAdmin.Web.Core.Filter;
using System.ComponentModel.DataAnnotations;
using System;
using System.IO;
using Newtonsoft.Json;
using System.Collections.Generic;
using StarsAdmin.Core.DB.Entities;
using System.Reflection;

namespace StarsAdmin.WebApi.Conttrollers
{
    /// <summary>
    /// 登录管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Order(1)]
    public class AuthController : BaseController
    {
        private readonly IAuthService _authService;

        public AuthController(IAuthService authService)
        {
            _authService = authService;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [LoginLogFilter]
        [AllowAnonymousFilter]
        public async Task<ResultModel<CurrentUserDto>> LoginAsync(LoginInput input)
        {
            return (await _authService.LoginAsync(input)).ResultFormat("登陆失败，请检查用户名和密码是否正确！");
        }

        /// <summary>
        /// 刷新Token
        /// </summary>
        /// <param name="refreshToken"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResultModel<string>> RefreshTokenAsync([Required] string refreshToken)
        {
            return (await _authService.GetRefreshTokenAsync(refreshToken)).ResultFormat("获取刷新Token失败");
        }

        /// <summary>
        /// 获取用户信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<CurrentUserDto>> GetUserInfoAsync()
        {
            return (await _authService.GetUserInfoAsync()).ResultFormat("获取用户信息失败");
        }

        /// <summary>
        /// 根据用户Id获取权限菜单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<MenuDto>>> GetAuthMenusAsync([Required] long id)
        {
            return (await _authService.GetAuthMenusAsync(id)).ResultFormat("获取菜单权限信息失败");
        }

        /// <summary>
        /// 根据用户Id获取权限按钮
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<MenuDto>>> GetAuthButtonsAsync([Required] long id)
        {
            return (await _authService.GetAuthButtonsAsync(id)).ResultFormat("获取按钮权限信息失败");
        }

        /// <summary>
        /// 根据用户Id获取按钮权限字段
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ResultModel<List<string>>> GetAuthButtonPermissionsAsync([Required] long id)
        {
            return (await _authService.GetAuthButtonPermissionsAsync(id)).ResultFormat("获取按钮权限字段信息失败");
        }
    }
}